package com.cn.jdia.sc;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.cn.jdia.db.DataSync;
import com.cn.jdia.db.RemarkHistory;
import com.cn.jdia.utils.CpuUtils;
import com.cn.jdia.utils.FileUtils;
import com.cn.jdia.utils.MemoryUtils;

public class SystemCollect {
	
	private static final Long inter=1000*60*10L;
	
	public static void main(String[] args) {
		try {
			Properties prop = new Properties();
			InputStream in = new BufferedInputStream(new FileInputStream("config.properties"));
			prop.load(in);

			Iterator<String> it = prop.stringPropertyNames().iterator();
			Map<String, String> project = new HashMap<>();
			while (it.hasNext()) {
				String key = it.next();
				// System.out.println(key + ":" + prop.getProperty(key));
				project.put(key, prop.getProperty(key));
			}
			in.close();

			// write down the last record
			RemarkHistory.writedown(project);
			
			while (true) {
				Map<String, Object> memory = MemoryUtils.memory();
				List<String> cpu = CpuUtils.sample();
				Map<String, Integer> disk = FileUtils.disk();
				DataSync.sync(project, memory, cpu, disk);
				
				System.out.println("Sleep...");
				Thread.sleep(inter);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
